/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Map;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import net.infonode.util.Direction;
import org.opu.db_vdumper.actions.view.ViewCorrectAction;
import org.opu.db_vdumper.beans.ApplicationData;
import org.opu.db_vdumper.io.FileIO;
import org.opu.db_vdumper.presenters.EventBus;
import org.opu.db_vdumper.resources.ConfigPath;
import org.opu.db_vdumper.resources.JProperties;
import org.opu.db_vdumper.ui.DbVDumperMenuMaker;
import org.opu.db_vdumper.ui.DbVDumperToolbarMaker;
import org.opu.db_vdumper.ui.DbVDumperView;
import org.opu.db_vdumper.ui.i18n.MenuI18nImpl;
import org.opu.db_vdumper.util.Logger;

/**
 * 
 * @author Potochnak Yana
 */
public class App {

    private DbVDumperView view;
    private ApplicationData data;
    private EventBus events;

    public App() {
        // init L&F
        JProperties prop = null;
        try {
            prop = new JProperties(FileIO.load(ConfigPath.getInstance().getFrameProp(), prop));
        } catch (IOException ex) {
            Logger.getInstance().info(App.class, "When load properties ");
            Logger.getInstance().info(App.class, ex);
        }
        
        try {
            UIManager.setLookAndFeel(prop.getProperty(DbVDumperView.LAF,
                    UIManager.getSystemLookAndFeelClassName()));
        } catch (Exception ex) {
            Logger.getInstance().warning(App.class, "Can't set l&f: ");
            Logger.getInstance().warning(App.class, ex);
        }

        // init variables
        view = new DbVDumperView(prop);
        data = new ApplicationData(view);
        events = new EventBus(data, view);

        // add additional window listeners
        events.addExitListeners(view.getFinalActions());
        view.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                events.getExitAction().actionPerformed(null);
            }
        });

        // adding additional ui controls : menu, toolbar.
        DbVDumperMenuMaker mm = new DbVDumperMenuMaker(events, new MenuI18nImpl(), view);
        view.setJMenuBar(mm);

        DbVDumperToolbarMaker tb = new DbVDumperToolbarMaker(events) ;
        view.getContentPane().add(tb, BorderLayout.NORTH);
        
        // synchronize ui controls
        Map<String, JMenuItem> toggles = mm.getToggles();
        
        JMenuItem get = toggles.get(ViewCorrectAction.ENABLE_CLOSE);
        if (get != null){
            get.setSelected(view.isCloseButtonEnable());
        }

        get = toggles.get(ViewCorrectAction.FREZE_LAYOUT);
        if (get != null){
            get.setSelected(view.isFreezeLayout());
        }

        direct(ViewCorrectAction.TOOGLE_DOWN, toggles);
        direct(ViewCorrectAction.TOOGLE_LEFT, toggles);
        direct(ViewCorrectAction.TOOGLE_RIGHT, toggles);
        direct(ViewCorrectAction.TOOGLE_UP, toggles);

        events.getExecuteQueryAction().setTextResource(view.getTextResource());
        view.setDbActions(events.dbActions());
    }

    private void direct(String name, Map<String, JMenuItem> toggles){
        JMenuItem get = toggles.get(name);
        if (get != null){
            Direction d = ViewCorrectAction.MAP.get(name);
            get.setSelected(view.isToogleEnabled(d));
        }
    }

    public void show(){
        view.setVisible(true);
    }


    public static void main(String[] args) {
        App app = new App();
        app.show();
    }

}
